/*
 * @Author: your name
 * @Date: 2021-08-24 10:36:25
 * @LastEditTime: 2022-07-13 11:41:18
 * @LastEditors: wei_jt@ecidi.com
 * @Description: In User Settings Edit
 * @FilePath: 接口加密
 */
import { interfaceEncryptType } from '@/config'
let type = interfaceEncryptType == 'SM2' ? 'sm4' : interfaceEncryptType.toLowerCase()
let _encrypt = null
import('../encryption/' + type).then(res => {
  _encrypt = res[interfaceEncryptType]
})


export function encrypt (config) {
  if (_encrypt) {
    if (config.data) {
      config.data = _encrypt.encrypt((config.data))
    }

    if (config.params) {
      config.params = { data: _encrypt.encrypt(JSON.stringify(config.params)) }
    }
  }

  if (config.method != 'get' && config.method != 'GET') {
    config.headers['Content-Type'] = 'application/json;charset=UTF-8'
  }
  return config
}
export function decrypt (response) {

  if (_encrypt && response.data) {
    response.data = _encrypt.decrypt(response.data)
  }
  return response
}